package org.example.service.impl;

import com.google.gson.Gson;
import jakarta.annotation.Resource;
import org.example.mapper.HomePageMapper;
import org.example.pojo.MonthlyTaskStats;
import org.example.service.HomePageService;
import org.example.utils.CharData;
import org.example.utils.SeriesData;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * @Description:
 * @Author:曹震
 * @Date: 2024/12/4 13:54
 */
@Service
public class HomePageServiceImpl implements HomePageService {
    @Resource
    private HomePageMapper mapper;


    @Override
    public int accountVolunteer() {
        int i = mapper.accountV();
        return i;
    }

    @Override
    public int accountTask() {
        int i = mapper.accountTasks();
        return i;
    }

    @Override
    public int accountInspectTask() {
        int i = mapper.accountInspectTask();
        return i;
    }


    @Override
    public String TaskAppear() {
        // 从 Mapper 获取数据
        List<MonthlyTaskStats> stats = mapper.findMonthlyTaskStats();

        // 准备 xAxis 数据（日期）
        List<String> days = new ArrayList<>();
        List<Integer> status3Counts = new ArrayList<>();

        List<Integer> totalCounts = new ArrayList<>();

        for (MonthlyTaskStats stat : stats) {
            days.add(stat.getDay());
            status3Counts.add(stat.getTaskCountStatus3OrMore());

            totalCounts.add(stat.getTotalTasks());
        }

        // 创建 SeriesData
        SeriesData series1 = new SeriesData("已完成的任务", status3Counts, "line", true, "red");

        SeriesData series3 = new SeriesData("所有任务", totalCounts, "line", true, "blue");

        // 创建 CharData
        CharData charData = new CharData();
        charData.setXAxis(days);
        charData.setSeriesData(Arrays.asList(series1, series3));

        // 转为 JSON
        Gson gson = new Gson();
        return gson.toJson(charData);
    }
}
